package com.lbn.jfqsc.app.recruitment.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lbn.baseframe.image.LoaderImage;
import com.lbn.baseframe.utils.UIHandler;

import java.util.List;

/**
 * Created by Administrator on 2016/6/14.
 */
public class ADAdapter extends PagerAdapter {

	private List<Integer> viewlist;

	private Context context;

	private AutoPlayRunnable mAutoPlayRunnable;

	private ViewPager mViewPager;

	private int mChildCount = 0;

	public ADAdapter(Context context, List<Integer> viewlist,
					 ViewPager mViewPager) {
		this.context = context;
		this.viewlist = viewlist;
		this.mViewPager = mViewPager;
		mAutoPlayRunnable = new AutoPlayRunnable();
		mViewPager.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					mAutoPlayRunnable.stop();
				} else if (event.getAction() == MotionEvent.ACTION_UP
						|| event.getAction() == MotionEvent.ACTION_CANCEL) {
					mAutoPlayRunnable.start();
				}
				return false;
			}
		});
	}

	@Override
	public int getCount() {
		// 设置成最大，使用户看不到边界
		return Integer.MAX_VALUE;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// Warning：不要在这里调用removeView
		((ViewPager) container).removeView((View)object);
	}

	@Override
	public ImageView instantiateItem(ViewGroup container, int position) {
		// 对ViewPager页号求模取出View列表中要显示的项

		if (viewlist != null) {
			if (viewlist.size() > 0) {
				position %= viewlist.size();
			}

			if (position < 0) {
				position = viewlist.size() + position;
			}

			ImageView view = new ImageView(context);
			view.setLayoutParams(new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT));
			view.setScaleType(ImageView.ScaleType.FIT_XY);
//			final Object data = viewlist.get(position%viewlist.size());
			final Integer data = viewlist.get(position%viewlist.size());
//			LoaderImage.displayImage(data.getImg(),view);
			Glide.with(context)
					.load(data)
					.fitCenter()
					.diskCacheStrategy(DiskCacheStrategy.RESULT)
					.crossFade()
					.into(view);
			// 如果View已经在之前添加到了一个父组件，则必须先remove，否则会抛出IllegalStateException。
			ViewParent vp = view.getParent();
			if (vp != null) {
				ViewGroup parent = (ViewGroup) vp;
				parent.removeView(view);
			}

			container.addView(view);

			return view;
		} else {
			return null;
		}
	}

	public void setData(List<Integer> viewlist) {
		if(this.viewlist!=null)
		{
			this.viewlist.clear();
		}
		this.viewlist = viewlist;
		mChildCount = getCount();
		this.notifyDataSetChanged();
	}

	@Override
	public int getItemPosition(Object object) {
		if (mChildCount > 0) {
			mChildCount--;
			return POSITION_NONE;
		}
		return super.getItemPosition(object);
	}

	private class AutoPlayRunnable implements Runnable {
		private int AUTO_PLAY_INTERVAL = 5000;
		private boolean mShouldAutoPlay;

		public AutoPlayRunnable() {
			mShouldAutoPlay = false;
		}

		public void start() {
			if (!mShouldAutoPlay) {
				mShouldAutoPlay = true;
				UIHandler.remove(this);
				UIHandler.postDelayed(this, AUTO_PLAY_INTERVAL);
			}
		}

		public void stop() {
			if (mShouldAutoPlay) {
				UIHandler.remove(this);
				mShouldAutoPlay = false;
			}
		}

		@Override
		public void run() {
			if (mShouldAutoPlay) {
				UIHandler.remove(this);
				int position = mViewPager.getCurrentItem();
				mViewPager.setCurrentItem(position + 1, true);
				UIHandler.postDelayed(this, AUTO_PLAY_INTERVAL);
			}
		}
	}

	/**
	 * 轮播图开始滑动
	 */
	public void start() {

		mAutoPlayRunnable.start();
	}

	/**
	 * 轮播图停止滑动
	 */
	public void stop() {

		mAutoPlayRunnable.stop();
	}
}
